package org.kineticsproject.visualizations;

import java.awt.Color;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYSeries;



public class XYSeriesExt extends XYSeries 
{
	
private String 			XUnits;
private String 			XLabel;
private String 			YUnits;
private String 			YLabel;
private Color  			labelColor;
private Color  			plotColor;
private double 			standardDeviation;
private double 			mean;
private double 			rangeMin;
private double 			rangeMax;
private boolean 		visible;
private int				index;

private double			maximum;
private double          minimum;
private boolean         autoRange;
private double          first;
private double          last;


public void setRange(double rangeMin, double rangeMax)
{
this.rangeMin= rangeMin;
this.rangeMax = rangeMax;
}
public double getRangeMax() {
	return rangeMax;
}

public void setRangeMax(double rangeMax) {
	this.rangeMax = rangeMax;
}

public double getRangeMin() {
	return rangeMin;
}

public void setRangeMin(double rangeMin) {
	this.rangeMin = rangeMin;
}

public Color getPlotColor() {
	return plotColor;
}

public void setPlotColor(Color plotColor) {
	this.plotColor = plotColor;
}

public Color getlabelColor() {
	return labelColor;
}

public void setlabelColor(Color labelColor) {
	this.labelColor = labelColor;
}



public XYSeriesExt(java.lang.Comparable key)
{
	super(key);
    XUnits 		= "";
    YUnits 		= "";
    YLabel 		= "";
    XLabel 		= "";
    labelColor 	= Color.black;
    plotColor 	= Color.black;
    rangeMin    = 0.0;
    rangeMax    = 0.0;
    visible 	= true;
    index		= 0;
    autoRange   = false;
    
}

public XYSeriesExt(java.lang.Comparable key, String XUnits, String YUnits, String XLabel, String YLabel)
{
	super(key);
    this.XUnits = XUnits;
    this.YUnits = YUnits;
    this.YLabel = YLabel;
    this.XLabel = XLabel;
    this.labelColor 	= Color.black;
    this.plotColor 		= Color.black;
    visible				= true;
    index				= 0;
    
}

public XYSeriesExt(java.lang.Comparable key, String XUnits, String YUnits, String XLabel, String YLabel, Color labelColor, Color plotColor)
{
	super(key);
    this.XUnits 		= XUnits;
    this.YUnits 		= YUnits;
    this.YLabel 		= YLabel;
    this.XLabel 		= XLabel;
    this.labelColor 	= labelColor;
    this.plotColor 		= plotColor;
    visible				= true;
    index				= 0;
}


public String getXLabel() {
	return XLabel;
}

public void setXLabel(String label) {
	XLabel = label;
}

public String getXUnits() {
	return XUnits;
}

public void setXUnits(String units) {
	XUnits = units;
}

public String getYLabel() {
	return YLabel;
}

public void setYLabel(String label) {
	YLabel = label;
}

public String getYUnits() {
	return YUnits;
}

public void setYUnits(String units) {
	YUnits = units;
}

public Color getLabelColor() {
	return labelColor;
}

public void setLabelColor(Color labelColor) {
	this.labelColor = labelColor;
}

public double getMean() {
	return mean;
}

public void setMean(double mean) {
	this.mean = mean;
}

public double getStandardDeviation() 
{
	return standardDeviation;
}

public List getYItems()
{
	XYDataItem XYItem;
	List<Number> yItems = new LinkedList<Number>();
	Iterator it = this.data.iterator();
	
	
	while (it.hasNext())
	{
		XYItem = (XYDataItem)it.next();
		yItems.add(XYItem.getY());
	}
	
	return(yItems);
}


public double getMaximum()
{
	return maximum;
}
public double getMinimum()
{
	return minimum;
}
/*
 * Calculate the Standard Deviation, Mean, Maximum, and Minimum
 */
public void calculateStatistics()
{

	
	double 		sumofSquares	= 0.0;
	double 		max          	= 0.0;
	double 		min          	= 0.0;
	boolean 	bFirst      	= true;
	
	
	Number item = null;
	Iterator it = this.getYItems().iterator();
	
	this.mean = org.jfree.data.statistics.Statistics.calculateMean(this.getYItems());
	
	while (it.hasNext())
	{
		item 			= (Number)it.next();
		sumofSquares	= sumofSquares + ((item.doubleValue()-mean)*(item.doubleValue()-mean));
		
		/* Calculate Maximum and Minimum here, it is basically a freebee being we're already traversing the list */
		if (bFirst)
		{
			first = max = min = item.doubleValue(); // seed the min & max values
			bFirst = false;
			
		}
		else
		{
			if (item.doubleValue() > max)
				max = item.doubleValue();
			if (item.doubleValue() < min)
				min = item.doubleValue();
		}

	}
	
	if (item != null)
		this.last = item.doubleValue();

	this.standardDeviation = (Math.sqrt((sumofSquares/(this.getYItems().size()-1))));
	this.maximum = max;
	this.minimum = min;
}

public void removeYMean()
{
	XYDataItem XYItem;
	Iterator it = this.data.iterator();
	
	while(it.hasNext())
	{
		XYItem = (XYDataItem)it.next();
		XYItem.setY(XYItem.getY().doubleValue()-this.getMean());
	}
}
public boolean isVisible()
{
	return visible;
}
public void setVisible(boolean visible)
{
	this.visible = visible;
}
public int getIndex()
{
	return index;
}
public void setIndex(int index)
{
	this.index = index;
}

public boolean isAutoRange() {
	return autoRange;
}
public void setAutoRange(boolean autoRange) {
	this.autoRange = autoRange;
}
public double getFirst()
{
	return first;
}
public double getLast()
{
	return last;
}


}



